<template>
  <div>
    <el-card style="width: 500px; margin: 50px auto">
      <h1 style="width:100px;margin: 15px auto">用户注册</h1>
      <el-form :model="registerForm" ref="registerForm" :rules="rules" label-width="160px">
        <!-- 头像 -->
        <el-form-item label="头像">
          <el-upload
            class="avatar-uploader"
            action="http://localhost:9527/aliyun/oss/upload"
            :headers="headers"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload">
            <img v-if="registerForm.avatar" :src="registerForm.avatar" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>

        <el-form-item prop="userName" label="用户名:">
          <el-input v-model="registerForm.username" style="width: 220px" placeholder="请输入用户名"/>
        </el-form-item>
        <el-form-item prop="password" label="密码:">
          <el-input v-model="registerForm.password" style="width: 220px" placeholder="请输入密码" show-password/>
        </el-form-item>
        <el-form-item prop="password2" label="确认密码:">
          <el-input v-model="registerForm.password2" style="width: 220px" placeholder="请再次输入密码" show-password/>
        </el-form-item>
        <el-form-item prop="nickname" label="昵称:">
          <el-input v-model="registerForm.nickname" style="width: 220px" placeholder="请输入昵称"/>
        </el-form-item>
        <el-form-item prop="phone" label="手机号码:">
          <el-input v-model="registerForm.phone" style="width: 220px" placeholder="请输入手机号"/>
        </el-form-item>
        <el-form-item prop="gender" label="性别:">
          <el-radio v-model="registerForm.gender" label="1">男</el-radio>
          <el-radio v-model="registerForm.gender" label="2">女</el-radio>
        </el-form-item>
        <el-form-item prop="birthday" label="生日:">
          <el-date-picker
            v-model="registerForm.birthday"
            type="date"
            placeholder="选择日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submit">注册</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { register } from '@/api/user'

export default {
  name: "register",
  created() {
  },
  data() {

    let validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      } else {
        if (this.registerForm.password !== '') {
          this.$refs.registerForm.validateField('checkPass');
        }
        callback();
      }
    };
    let validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.registerForm.password) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };

    return {
      registerForm: {
        username: '',
        password: '',
        password2: '',
        nickname:  '',
        phone: '',
        avatar: '',
        gender:  '1',
        birthday:  '',
      },
      rules: {
        userName: [
          { required: true, message: '用户名不能为空', trigger: 'blur'}
        ],
        password: [
          { required: true, validator: validatePass, trigger: 'blur'}
        ],
        password2: [
          { required: true, validator: validatePass2, trigger: 'blur'}
        ],
        nickname: [
          { required: true, message: '昵称不能为空', trigger: 'blur'}
        ],
        phone: [
          {required: true, message: '手机号码不能为空', trigger: 'blur'}
        ],
        gender: [
          {required: true, message: '性别必须选择', trigger: 'blur'},
        ],
        birthday: [
          {required: true, message: '请选择你的生日', trigger:' blur'}
        ],
        avatar: [
          {required: true, message: '请选择上传头像', trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    // uploadSuccess(res, file){
    //   this.registerForm.avatar =  URL.createObjectURL(file.raw);
    // },
    submit(){
      this.$refs.registerForm.validate((v) => {
        if (v) {
          //TODO 发请求注册用户
          register(this.registerForm).then(() => {
            this.$message({
                type: 'success',
                message: '注册成功!'
            })
            this.$router.push({ path: this.$route.query.redirect || '/' })
          }).catch(() => {
            this.$message.error('注册失败');
          })
          console.log(this.registerForm)
        } else {
          return false
        }
      })
    },
    //头像上传
    handleAvatarSuccess(res, file) {
        this.registerForm.avatar = res.data
    },
    beforeAvatarUpload(file) {
        // const isJPG = file.type === 'image/jpeg';
        const isLt2M = file.size / 1024 / 1024 < 2;

        // if (!isJPG) {
        //   this.$message.error('上传头像图片只能是 JPG 格式!');
        // }
        if (!isLt2M) {
          this.$message.error('上传头像图片大小不能超过 2MB!');
        }
        return isLt2M;
    },
  }
}
</script>

<style scoped>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 100px;
    height: 100px;
    line-height: 100px;
    text-align: center;
  }
  .avatar {
    width: 100px;
    height: 100px;
    display: block;
  }
</style>
